package com.hc.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hc.base.BaseQuery;
import com.hc.base.ResultEnum;
import com.hc.base.ResultVo;
import com.hc.domain.Menu;
import com.hc.domain.User;
import com.hc.domain.vo.LoginVo;
import com.hc.service.UserService;
import com.hc.utils.EncryptionUtil;
import com.hc.utils.MenuTreeUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 系统用户 前端控制器
 * </p>
 *
 * @author wzf
 * @since 2024-09-10
 */
@RestController
@RequestMapping("/user")
@Tag(name = "用户控制器")
@Slf4j
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 登录接口
     * @param user
     * @return
     * @throws NoSuchAlgorithmException
     */
    @PostMapping("login")
    @Operation(summary = "登录接口")
    public ResultVo doLogin(@RequestBody User user) throws NoSuchAlgorithmException {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("username",user.getUsername());
        User one = userService.getOne(wrapper);
        if (Objects.isNull(one)){
            return ResultVo.error().message(ResultEnum.NO_USER_FOUND.getMessage());
        }
        String password = user.getPassword();
        if(StringUtils.isBlank(password)){
            return ResultVo.error().message(ResultEnum.PWD_NULL.getMessage());
        }
        String encryPwd = EncryptionUtil.hashWithSHA256(password);
        if(!encryPwd.equals(one.getPassword())){
            return ResultVo.error().message(ResultEnum.PWD_ERROR.getMessage());
        }
        StpUtil.login(user.getUsername());
        LoginVo loginVo = new LoginVo();
        String tokenValue = StpUtil.getTokenInfo().tokenValue;
        log.info("login create token:"+tokenValue);
        loginVo.setAuthorization(tokenValue);
        one.setPassword("");
        loginVo.setCurrentUser(one);
        //获取用户菜单权限
        List<Menu> menuList = getMenuList(user.getUsername());
        loginVo.setMenuList(menuList);
        return ResultVo.ok().data(loginVo);
    }


    /**
     * 获取用户列表
     */
    @PostMapping("list")
    @Operation(summary = "获取用户列表")
    public  ResultVo<List<User>> listUser(@RequestBody BaseQuery query){
        User user =new User();


        List<User>  users = userService.listUser(user);
        return ResultVo.ok(users);
    }

    /**
     * 退出登录接口
     */
    @GetMapping("logout")
    @Operation(summary = "退出登录接口")
    public ResultVo loginOut(@RequestHeader String token){
        log.info("loginout........."+token);
        StpUtil.logout();
        return ResultVo.ok();
    }



    /**
     * 获取用户菜单
     */
    private List<Menu> getMenuList(String userName){
        List<Menu>  menus =userService.listMenus(userName);
        List<Menu> menuList = MenuTreeUtil.makeListToTree(menus);
        return menuList;
    }

}
